package ee.rsi2m.goproject.model;

import ee.rsi2m.goproject.util.ICanvasCommand;
import ee.rsi2m.goproject.util.Point;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;

public class GoStone implements ICanvasCommand {

	private float x;
	private float y;
	private Point point;
	private int relativeX;
	private int relativeY;
	private int TURN;
	private boolean isKo;
	private boolean COLOR;
	private Bitmap image;
	private boolean visibility;
	public Paint p;
	private GoGroup group;
	
	public boolean isKo() {
		return isKo;
	}

	public void setKo(boolean isKo) {
		this.isKo = isKo;
	}

	private boolean regroupNeeded;

	public boolean isRegroupNeeded() {
		return regroupNeeded;
	}

	public void setRegroupNeeded(boolean regroupNeeded) {
		this.regroupNeeded = regroupNeeded;
	}

	public GoStone(float x, float y) {
		this.x = x;
		this.y = y;

	}
	
	public int getBoardX() {
		return relativeX;
	}



	public int getRelativeX() {
		return relativeX;
	}

	public void setRelativeX(int relativeX) {
		this.relativeX = relativeX;
	}

	public int getRelativeY() {
		return relativeY;
	}

	public void setRelativeY(int relativeY) {
		this.relativeY = relativeY;
	}

	public void setBoardY(int boardY) {
		this.relativeY = boardY;
	}

	public Point getPoint() {
		return point;
	}

	public void setPoint(Point point) {
		this.point = point;
	}

	public GoStone(Point point, boolean visibility, Paint p) {
		super();
		this.point = point;
		this.x=point.getX();
		this.y=point.getY();
		this.visibility = visibility;
		this.p = p;
	}

	public GoStone(Point point){
		this.point = point;
	}
	
	public GoStone(){
		super();
	}
	
	

	public GoStone(float x, float y, boolean visibility, Paint p) {
		super();
		this.x = x;
		this.y = y;
		this.visibility = visibility;
		this.p = p;
	}
	

	public void initialize(boolean Color, Bitmap image, int turn) {

		this.COLOR = Color;
		this.TURN = turn;
		this.image = image;

	}

	public void draw(Canvas canvas) {

		canvas.drawBitmap(this.image, this.x - image.getWidth() / 2,
				this.y - image.getHeight() / 2, p);

	}

	/*
	 * public boolean isVisible() { return this.visibility; }
	 */



	public float getX() {
		return this.x;
	}

	public boolean isVisibilible() {
		return visibility;
	}

	public void setVisibility(boolean visibility) {
		this.visibility = visibility;
	}

	public float getY() {
		return this.y;
	}

	public void setX(float x) {
		this.x = x;
	}
	
	public boolean isLogicNeigbour(GoStone stone){
		return this.COLOR==stone.COLOR;
	}

	public GoGroup getGroup() {
		return group;
	}

	public void setGroup(GoGroup group) {
		this.group = group;
	}

	public void setY(float y) {
		this.y = y;
	}

	public int getTurn() {
		return this.TURN;
	}

	public static Bitmap getResizedBitmap(Bitmap bm, float f) {

		int width = bm.getWidth();

		int height = bm.getHeight();

		float aspect = (float) width / height;

		float scaleWidth = f;

		float scaleHeight = scaleWidth / aspect; // yeah!

		// create a matrix for the manipulation

		Matrix matrix = new Matrix();

		// resize the bit map

		matrix.postScale(scaleWidth / width, scaleHeight / height);

		// recreate the new Bitmap

		Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
				matrix, true);

		bm.recycle();

		return resizedBitmap;
	}

	public void undo() {
		this.setVisibility(false);
	}

	public GoStone(float x, float y, boolean visibility, boolean isKo) {
		super();
		this.x = x;
		this.y = y;
		this.visibility = visibility;
		this.isKo = isKo;
	}

	public boolean isCOLOR() {
		return COLOR;
	}

	public void setCOLOR(boolean cOLOR) {
		COLOR = cOLOR;
	}

	@Override
	public String toString() {
		return "X:"+relativeX+"Y:"+relativeY;
	}
	

}
